<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-button plain type="info" @click="openDialog">新增学科</el-button>
      <!-- 对话框 -->
      <el-dialog title="新增学科" :visible.sync="dialogVisible">
        <el-form >
          <el-form-item label="*学科名称:" >
            <el-input v-model="subjectName" 
            style="width:70%" 
            size="small"
            placeholder="请输入"
            :name="subjectName"></el-input>
          </el-form-item>
          <el-form-item label="*是否显示:">
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="display">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button plain size="small" @click="dialogVisible=false" style="float:right">取消</el-button>
            <el-button type="primary" size="small" style="float:right;marginRight:20px" @click="submit">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 搜索 -->
      <div class="search">
        <span>学科名称</span>
        <el-input v-model="keywords" 
          type="text"
          placeholder="请输入"
          style="width:35%"
          size="small">
        </el-input>
        <el-button type="primary" size="small" style="float:right" @click="search">搜索</el-button>
        <el-button plain size="small" style="float:right;marginRight:15px" @click="clear">清除</el-button>
      </div>
      <!-- 列表 -->
      <div class="sub_list" style="marginTop:20px">
        <el-table
          :data="subjects"
          height="250"
          border
          style="width: 100%">
          <el-table-column
            label="序号"
            type="index"
            :index="1">
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="学科名称"
            width="120">
          </el-table-column>
          <el-table-column
            prop="creator"
            label="创建者"
            width="120">
          </el-table-column>
          <el-table-column
            label="创建日期"
            width="120">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTime}}
            </template>
          </el-table-column>
          <el-table-column
            label="前台是否显示"
            width="120">
            <template slot-scope="scope">
              <div v-if="scope.row.isFrontDisplay===1">是</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="120">
          </el-table-column>
          <el-table-column
            prop="tags"
            label="标签"
            width="120">
          </el-table-column>
          <el-table-column
            prop="totals"
            label="题目数量"
            width="120">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/subjects/directorys')">学科分类</el-button>
              <el-button type="text" size="small" @click="$router.push('/subjects/tags')">学科标签</el-button>
              <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>
              <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
        class="pager"
        background
        layout="prev, pager, next, sizes, jumper"
        :page-size="reqParams.pagesize"
        :current-page="reqParams.page"
        :page-sizes="[1,2,3,4]"
        :total="total"
        @size-change="sizeChange"
        @current-change="changePage">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import * as subs from '../../api/hmmm/subjects'
export default {
  name: 'SubjectsList',
  created() {
    this.getSubjects()
  },
  data() {
    return {
      reqParams: {
        page: 1,
        pagesize: 2
      },
      subjectName: null,
      dialogVisible: false,
      formatLabelWidth: '30px',
      value: true,
      keywords: null,
      subjects: [],
      total: 0
    }
  },
  methods: {
    // 弹出对话框
    openDialog () {
      this.dialogVisible = true
      this.value = true
    },
    // switch组件显示对话框
    display(data) {
      if (!data) {
        this.dialogVisible = data
      }
    },
    // 确认
    async submit () {
      await subs.add(this.reqParams.subjectName)
      this.dialogVisible = false
      this.subjectName = null
    },
    // 清除
    clear () {
      this.keywords = null
    },
    // 获取学科列表
    async getSubjects () {
      const {data} = await subs.list({params: this.reqParams})
      this.subjects = data.items
      this.total = data.counts
    },
    // 编辑
    async edit(data) {
      await subs.update(data)
      this.$message.success('修改成功')
      this.getSubjects()
    },
    // 删除
    del (data) {
      this.$confirm('是否删除？', '温馨提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        await subs.remove(data)
        this.$message.success('删除成功')
        this.getSubjects()
      }).catch(() => {

      })
    },
    // 分页
    changePage (newpage) {
      this.reqParams.page = newpage
      this.getSubjects()
    },
    // 搜索
    search () {
      let reg = new RegExp(this.keywords.trim())
      let arr = []
      let sjs = this.subjects
      for (let i = 0; i < sjs.length; i++) {
        if (reg.test(sjs[i].subjectName)) {
          arr.push(sjs[i])
        }
      }
      this.subjects = arr
      this.total = arr.length
      this.keywords = null
      this.changePage()
    },
    sizeChange (size) {
      this.reqParams.pagesize = size
      this.getSubjects()
    }
  }
}
</script>

<style scoped>
.search {
  margin-top: 20px;
}
.search span {
  font-size: 14px;
}
</style>
